package org.jmc.refmanager.loader.util;

import org.apache.log4j.Logger;

public class Util {
	private static Logger loaderLogger = Logger.getLogger("LOADER");
	private static Logger errorLogger = Logger.getLogger("ERROR");
	public static final String BLANK = "";
	public static final String NULL = "null";

	/**
	 * 
	 * @param value
	 * @return
	 */
	public static String getBlankIfNull(String value) {
		if (isEmptyOrNull(value)) {
			return BLANK;
		} else {
			return value;
		}
	}

	/**
	 * 
	 * @param value
	 * @return
	 */
	public static boolean isEmptyOrNull(String value) {
		return (value == null || value.trim().isEmpty());
	}
	
	/**
	 * 
	 * @param argName
	 * @param argValue
	 */
	public static void checkRequired(String argName, String argValue) {
		if (isEmptyOrNull(argValue)) {
			throw new IllegalArgumentException("La valeur du parametre '" + 
					argName + "' ne peut pas etre vide.");
		}
		
	}
	/**
	 * 
	 * @return
	 */
	public static boolean isDebugEnabled() {
		return loaderLogger.isDebugEnabled();
	}
	
	/**
	 * 
	 * @param message
	 */
	public static void trace(String message) {
		trace(message, (Object[]) null);
	}
	
	/**
	 * 
	 * @param message
	 * @param args
	 */
	public static void trace(String message, Object... args) {
		if (isDebugEnabled()) {
			loaderLogger.debug(String.format(message, args));
		}
	}

	/**
	 * 
	 * @param message
	 * @param args
	 */
	public static void info(String message, Object... args) {
		if (loaderLogger.isInfoEnabled()) {
			loaderLogger.info(String.format(message, args));
		}
	}

	/**
	 * 
	 * @param message
	 * @param args
	 */
	public static void error(String message, Object... args) {
		loaderLogger.error(String.format(message, args));
	}

	/**
	 * 
	 * @param e
	 */
	public static void traceException(Exception e) {
		loaderLogger.error(e.getMessage());
		errorLogger.error("Erreur de chargement", e);
	}
	
}
